package app.controller

import spark.Request
import spark.Response
import app.service.UserService
import org.jooq.tools.json.JSONObject

object UserController: BaseController() {
    val user_service = UserService()

    // 列表
    val list = { _: Request, _: Response ->
        val ret_list = user_service.getList()
        if (ret_list == null) render.error(user_service.error)
        render.response("获取成功", ret_list)
    }

    // 信息
    val info = { req: Request, _: Response ->
        val uid = req.params("id").toInt()
        val ret_data = user_service.getUser(uid)
        if (ret_data == null) render.error(user_service.error)
        render.response("获取成功", ret_data)
    }

    // 新增
    val create = { req: Request, _: Response ->
        val params = JSONObject()
        params.put("phone", req.queryParams("phone"))
        params.put("openid", req.queryParams("openid"))
        val ret_data = user_service.create(params)
        if (ret_data == false) render.error(user_service.error)
        render.response("操作成功", ret_data)
    }

    // 更新
    val update = { req: Request, _: Response ->
        val uid = req.params("id").toInt()
        val params = JSONObject()
        params.put("nickname", req.queryParams("nickname"))
        val ret_data = user_service.update(uid, params)
        if (!ret_data) render.error(user_service.error)
        render.response("操作成功", ret_data)
    }

    // 删除
    val delete = { req: Request, _: Response ->
        val uid = req.params("id").toInt()
        val ret_data = user_service.delete(uid)
        if (!ret_data) render.error(user_service.error)
        render.response("操作成功", ret_data)
    }
}